App.controller('alaryAnalyzeController', ['$rootScope', '$scope', '$state', 'Page','$interval', function($rootScope, $scope, $state, Page, $interval) {

	// 查询工资单的年份
	 Page.ajaxGet('salaryPayRoll/getSalaryPayRollYear.do', null, function(resp) {
	        $scope.salaryPayRollYearList = resp.data;
	    });
	 
	 /*$("#empNameInput").on("focus",function(){// 输入框获得焦点
		 var empName= $("#empNameInput").val();
         if(empName == undefined || empName == "") {
        	 $("#empArrayDiv").css('display','none');
         }else {
        	 $("#empArrayDiv").css('display','');
         }
     });*/
	 
	/* $("#empNameInput").on("blur",function(){// 输入框失去焦点
		 $("#empArrayDiv").css('display','none');
     });*/
	
	var flag = false;
 	$("#empNameInput").on("keyup",function(){
		 flag = true;
		 $scope.inputTimer = $interval(function(){
			 $scope.manageData();// 显示数据
		 }, 200);// 时间显示0.2秒钟执行一次
 	});
 
     $("#empNameInput").on("keydown",function(){
     	$interval.cancel($scope.inputTimer); // 终止函数执行
     	flag = false;
 	});
     
     $scope.chooseEmp = function(emp){
    	 $scope.emp = emp;
    	 $scope.empName = $scope.emp.empName;
    	 $("#empArrayDiv").animate({height:'0px'});
    	 $("#empArrayDiv").css('display','none');
     }
     
     $scope.manageData = function() {
			if (flag) {
				$scope.emp = null;
				var empNameValue= $("#empNameInput").val();
	            if(empNameValue == undefined || empNameValue == "") {
	            	$scope.empArray = [];
	            	$("#empArrayDiv").animate({height:'0px'});
	            	$("#empArrayDiv").css('display','none');
	            } else {
	                Page.ajaxPostQuiet("salaryPayRollDetail/getEmpByEmpName.do", {"empName" : empNameValue.replace(/\s/g, "")}, function (resp) {
						var empName = resp.data.empName;// 标志位
						var empNameStr = empNameValue.replace(/\s/g, "")
						if(empName == empNameStr){// 同一次请求
							$scope.empArray = resp.data.empArray;
							if($scope.empArray.length == 0){
								$("#empArrayDiv").animate({height:'0px'});
				            	$("#empArrayDiv").css('display','none');
							}else {
								var height = 25 * $scope.empArray.length;
								$("#empArrayDiv").css('display','');
								var inputHeight=  $('#empNameInput').height();
								var inputTop = $('#empNameInput').position().top+inputHeight+13;
								var inputWidth= $('#empNameInput').width()+26;
								$("#empArrayDiv").css('width',inputWidth+'px');
								$("#empArrayDiv").css('top',inputTop+'px');
								$("#empArrayDiv").animate({height:height+'px'});
							}
		                    for (var i = 0; i <  $scope.empArray.length; i++) {
		                    	if($scope.empArray[i].empName.indexOf(empNameStr)>-1){
		                    		var deptName = $scope.empArray[i].deptName;
		                    		var empName = $scope.empArray[i].empName.replace(new RegExp(empNameStr,'gm'),"<span style=\"margin-left: 0px;color: #108ee9;font-family: PingFangSC-Regular;font-size: 15px;\">"+empNameStr+"</span>");// 替换全部
		                    		$scope.empArray[i].empNameHtml = empName+"  -- "+deptName+"";// 把值作为html
		                    	}
		                    }
						}
	                });
	            }
			}
			$interval.cancel($scope.inputTimer); // 终止函数执行
			flag = false;
		}
	 
	 $scope.highchartsObject = {
			 init : function(){
				 if($scope.emp==undefined){
					 Page.notice("请选择员工");
					 return false;
				 }
				 if($scope.payRollYear == undefined){
					 Page.notice("请选择年份");
					 return false;
				 }
				 var param = {};
				 param.payRollYear = $scope.payRollYear;
				 param.empId = $scope.emp.empId;
				 Page.ajaxGet('salaryPayRollDetail/analyzeEmpSalary.do', param, function(resp) {
				        $scope.paidSalaryArray = resp.data.paidSalaryArray;
				        $scope.deductionArray = resp.data.deductionArray;
				        Highcharts.chart('chartContainer', {
				    	    chart: {
				    	        type: 'column'
				    	    },
				    	    title: {
				    	        text: '员工收入分析'
				    	    },
				    	    subtitle: {
				    	        text: ' '
				    	    },
				    	    xAxis: {
				    	        categories: [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
				    	        crosshair: true
				    	    },
				    	    yAxis: {
				    	        min: 0,
				    	        title: {
				    	            text: '金额 (元)'
				    	        }
				    	    },
				    	    tooltip: {
				    	        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
				    	        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
				    	            '<td style="padding:0"><b>{point.y:.1f} 元</b></td></tr>',
				    	        footerFormat: '</table>',
				    	        shared: true,
				    	        useHTML: true
				    	    },
				    	    plotOptions: {
				    	        column: {
				    	            pointPadding: 0.2,
				    	            borderWidth: 0
				    	        }
				    	    },
				    	    series: [{
				    	        name: '实发工资',
				    	        data: $scope.paidSalaryArray

				    	    }, {
				    	        name: '提成',
				    	        data: $scope.deductionArray
				    	    }]
				    	});
				    });
			 },
			 reset : function(){
				 $scope.emp = null;
				 $scope.empName = null;
				 $scope.payRollYear = null;
			 }
		 }
}]);